package com.thx.app.remote.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mixedserver.protocol.RPCException;
import mixedserver.protocol.jsonrpc.client.Client;

import com.thx.app.remote.CappOpinionInterface;
import com.thx.app.remote.model.CappOpinionType;
import com.thx.app.remote.model.CappOpinion_;
import com.thx.appserver.model.CappOpinion;
import com.thx.appserver.service.CappOpinionManager;
import com.thx.common.spring.BeanFactory;
import com.thx.dictionary.model.Dictionary;
import com.thx.dictionary.model.DictionaryValue;
import com.thx.dictionary.service.DictionaryManager;
import com.thx.dictionary.service.DictionaryValueManager;

public class CappOpinionImp implements CappOpinionInterface {


	public Boolean submitOpinion(CappOpinion_ opinion) throws RPCException {
		try {
			if (opinion != null) {
				CappOpinion o = new CappOpinion();
				o.setContact(opinion.getContact());
				o.setDeviceId(opinion.getDeviceId());
				o.setImsi(opinion.getImsi());
				o.setMobileModel(opinion.getMobileModel());
				o.setOpinionContent(opinion.getOpinionContent());
				o.setOpinionType(opinion.getOpinionType());//code
				o.setUpdateDate(new Date());
				CappOpinionManager cappOpinionManager = (CappOpinionManager) BeanFactory.getBean("cappOpinionManager");
				cappOpinionManager.save(o);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<CappOpinionType> getOpinionTypes() throws RPCException {
		DictionaryManager dictionaryManager = (DictionaryManager) BeanFactory.getBean("dictionaryManager");
		DictionaryValueManager dictionaryValueManager = (DictionaryValueManager) BeanFactory.getBean("dictionaryValueManager");

		Dictionary dictionary = dictionaryManager.findByDicName("opinionType");
		if (dictionary != null) {
			List<DictionaryValue> values = dictionaryValueManager.findByDicnCode(dictionary.getDicnCode());
			if (values != null && values.size() > 0) {
				List<CappOpinionType> types = new ArrayList<CappOpinionType>();
				for (DictionaryValue v : values) {
					CappOpinionType type = new CappOpinionType();
					type.setId(v.getDicvId());
					type.setDicText(v.getDicvText());
					type.setDicCode(v.getDicValue());
					types.add(type);
				}
				return types;
			}

		}

		return null;
	}

	public static void main(String[] args) {
		Client client = Client
				.getClient("http://localhost:8080/simple/JSON-RPC?debug=true");
		client.setDencryptMessage(true);
		CappOpinionInterface inter = client.openProxy("opinion",
				CappOpinionInterface.class);
		try {

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}